package com.heima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.mapper.NodeMapper;
import com.heima.mapper.RegionMapper;
import com.heima.mapper.VendingMachineMapper;
import com.heima.pojo.Node;
import com.heima.pojo.Region;
import com.heima.pojo.dto.AddRegionDTO;
import com.heima.pojo.dto.RegionDTO;
import com.heima.pojo.dto.RegionPageDTO;
import com.heima.pojo.dto.RegionSeleDTO;
import com.heima.pojo.vo.PageSearchQueryVO;
import com.heima.service.RegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RegionServiceImpl implements RegionService {

    @Autowired
    RegionMapper regionMapper;
    @Autowired
    NodeMapper nodeMapper;
    @Autowired
    VendingMachineMapper machineMapper;
/**
 * 区域搜索
 * **/
    @Override
    public PageSearchQueryVO pageQuery(RegionPageDTO dto) {
        //开启分页
        PageHelper.startPage(dto.getPageIndex(),dto.getPageSize());
        //查询数据库中的数据
        Page<Region> page = regionMapper.pageQuery(dto);
        for (Region region : page.getResult()) {
            Integer  nobeCount = nodeMapper.nobeCount(region.getId());
            region.setNodeCount(nobeCount);
          List<Node>list = regionMapper.getNodeId(region.getId());
          region.setNodeList(list);
        }
        //set出vo中的数据
        PageSearchQueryVO vo = new  PageSearchQueryVO();
        vo.setPageIndex(dto.getPageIndex());
        vo.setPageSize(dto.getPageSize());
        vo.setTotalPage(page.getPages());
        vo.setTotalCount((int) page.getTotal());
        vo.setCurrentPageRecords(page.getResult());
        return vo;
    }
    /**
     * 新增区域
     * **/
    @Override
    public void add(RegionDTO dto) {
        Region region = new Region();
        region.setName(dto.getRegionName());
        region.setRemark(dto.getRemark());
        regionMapper.add(region);
    }
    /**
     * 删除区域信息
     *
     * @return
     **/
    @Override
    public boolean deleteId(Long id) {
        regionMapper.deleteId(id);
        return true;
    }
    /**
     * 修改区域信息
     * **/
    @Override
    public void upd(Long regionId, AddRegionDTO dto) {
        Region region = new Region();
        region.setId(regionId);
        region.setName(dto.getRegionName());
        region.setRemark(dto.getRemark());
        regionMapper.upd(region);
    }
    /**
     * 查看区域详细
     * **/
//    @Override
//    public PageSearchQueryVO seleId(RegionSeleDTO dto) {
//        Region region = new Region();
//        nodeMapper.seleteById(dto);
//    }
}
